package com.bot.pms.common.config;

import com.bot.pms.common.config.dateConverter.DateConverter;
import com.bot.pms.common.config.dateConverter.LocalDateConverter;
import com.bot.pms.common.config.dateConverter.LocalDateTimeConverter;
import com.bot.pms.common.config.dateConverter.LocalTimeConverter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.TimeZone;

/**
 * webmvc配置类
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> converter : converters) {
            if(converter instanceof  MappingJackson2HttpMessageConverter){
                //获取MappingJackson2HttpMessageConverter 配置java8 Time 序列化模块
                mappingJackson2HttpMessageConverterSetup((MappingJackson2HttpMessageConverter) converter);
            }
        }
        super.extendMessageConverters(converters);
    }

    /**
     * 添加时间convertor
     * @param registry
     */
    @Override
    protected void addFormatters(FormatterRegistry registry) {
        LocalTimeConverter localTimeConverter = new LocalTimeConverter();
        registry.addConverter(localTimeConverter);

        LocalDateConverter localDateConverter = new LocalDateConverter();
        registry.addConverter(localDateConverter);

        DateConverter dateConverter = new DateConverter();
        registry.addConverter(dateConverter);

        LocalDateTimeConverter localDateTimeConverter = new LocalDateTimeConverter();
        registry.addConverter(localDateTimeConverter);
        super.addFormatters(registry);
    }



    /**
     * 配置MappingJackson2HttpMessageConverter java8Time序列化模块
     * @return
     */
    public void mappingJackson2HttpMessageConverterSetup(MappingJackson2HttpMessageConverter converter) {
        ObjectMapper objectMapper = converter.getObjectMapper();
        // 指定时区
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

        // 日期类型字符串处理
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        //关闭序列化为数字时间
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        //没有定义的属性就忽略
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //允许出现单引号
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true) ;
        //支持null和空串
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        //允许出现特殊字符和转义符
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;

        // Java8日期日期处理
        JavaTimeModule javaTimeModule = new JavaTimeModule();

        //日期反序列化，主要返回数据时使用
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        // 日期序列化，主要返回数据时使用
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        objectMapper.registerModule(javaTimeModule);
    }

}
